using System.Linq;
using FacebookClient;

namespace FacebookClient.Database
{
    partial class FacebookDatabaseDataContext
    {
        public void AddUser(User user)
        {
            var oldUser = LoadUser(user.Id);
            if (oldUser == null)
            {
                Users.InsertOnSubmit(user);
                SubmitChanges();
            }
        }

        public void AddComment(Comment comment)
        {
            var oldComment = Comments.FirstOrDefault(c => c.Id == comment.Id);
            if (oldComment == null)
            {
                Comments.InsertOnSubmit(comment);
                SubmitChanges();
            }
        }

        public User LoadUser(long uid)
        {
            return Users.FirstOrDefault(u => u.Id == uid);
        }
    }

    partial class Comment
    {
        public override string ToString()
        {
            string s = Id;
            s += '\t' + Content;
            s += '\t' + Source;
            s += '\t' + FromUserId;
            s += '\t' + ToUserId;
            s += '\t' + OwnerUserId;
            s += '\t' + Time;
            return s;
        }
    }
}
